﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ForguncyPluginCreator
{
    internal class PluginProjectCreator
    {
        public static void Create(string templateFolder, string targetFolder, MainWindow.MainWindowViewModel settings, string forguncyVersion)
        {
            templateFolder = Path.GetFullPath(templateFolder);
            targetFolder = Path.GetFullPath(targetFolder);
            var files = Directory.GetFiles(templateFolder, "*.*", SearchOption.AllDirectories);

            foreach (var item in files)
            {
                var relativePath = item.Substring(templateFolder.Length + 1);
                var targetRelativePath = GetReplacedStr(relativePath, settings, forguncyVersion);
                var targetPath = Path.Combine(targetFolder, targetRelativePath);
                var extension = Path.GetExtension(item).ToLower();

                if(NeedSkip(item, settings))
                {
                    continue;
                }

                EnsureFolder(targetPath);
                var textExtentions = new string[] { "cs", "js", "css", "ts", "sln", "csproj", "json", "xaml" };
                if (textExtentions.Any(i => extension == "." + i))
                {
                    var sourceContent = File.ReadAllText(item);

                    if (Path.GetFileName(item) == "PluginConfig.json")
                    {
                        sourceContent = UpdatePluginConfigContentByPluginType(sourceContent, settings);
                    }

                    var content = GetReplacedStr(sourceContent, settings, forguncyVersion);
                    File.WriteAllText(targetPath, content);
                }
                else
                {
                    File.Copy(item, targetPath);
                }
            }
        }

        private static string UpdatePluginConfigContentByPluginType(string sourceContent, MainWindow.MainWindowViewModel settings)
        {
            if (!settings.ContainCellType && !settings.ContainCommand)
            {
                sourceContent = sourceContent.Replace("    \"Resources/ForguncyPluginTemplateCellType.js\",\r\n    \"Resources/ForguncyPluginTemplateCommand.js\"", "");
            }
            if (!settings.ContainCellType)
            {
                sourceContent = sourceContent.Replace("    \"Resources/ForguncyPluginTemplateCellType.js\",\r\n    \"Resources/ForguncyPluginTemplateCommand.js\"", "    \"Resources/ForguncyPluginTemplateCommand.js\"");
                sourceContent = sourceContent.Replace("\"pluginType\": \"cellType,command\",", "\"pluginType\": \"command\",");
            }
            if (!settings.ContainCommand)
            {
                sourceContent = sourceContent.Replace("    \"Resources/ForguncyPluginTemplateCellType.js\",\r\n    \"Resources/ForguncyPluginTemplateCommand.js\"", "    \"Resources/ForguncyPluginTemplateCellType.js\"");
            }
            if (!settings.ContainServerCommand && !settings.ContainCommand)
            {
                sourceContent = sourceContent.Replace("\"pluginType\": \"cellType,command\",", "\"pluginType\": \"cellType\",");
            }
            return sourceContent;
        }

        private static bool NeedSkip(string item, MainWindow.MainWindowViewModel settings)
        {
            if (item.Contains(@".vs\") || item.Contains(".user") || item.Contains(@"bin\") || item.Contains(@"obj\"))
            {
                return true;
            }
            var fileName = Path.GetFileName(item);
            if (!settings.ContainCellType)
            {
                if (fileName.Contains("ForguncyPluginTemplateCellType"))
                {
                    return true;
                }
            }
            if (!settings.ContainCommand)
            {
                if (fileName.Contains("ForguncyPluginTemplateCommand"))
                {
                    return true;
                }
            }
            if (!settings.ContainServerCommand)
            {
                if (fileName.Contains("ForguncyPluginTemplateServerCommand"))
                {
                    return true;
                }
            }
            if (!settings.ContainCellType && !settings.ContainCommand)
            {
                if (fileName.Contains(".d.ts"))
                {
                    return true;
                }
            }
            return false;
        }

        public static void EnsureFolder(string file)
        {
            var path = Path.GetDirectoryName(file);
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
        }
        public static string GetReplacedStr(string str, MainWindow.MainWindowViewModel settings, string forguncyVersion)
        {
            var forguncyFolder = settings.ForguncyInstallPath;
            var packageToolPath = Path.GetFullPath("ForguncyPluginPackageTool.exe");
            return str
                .Replace("%PackageToolPath%", packageToolPath)
                .Replace("%ForguncyInstallPath%", forguncyFolder)
                .Replace("ForguncyPluginTemplate-LacaleName", settings.LocaleName ?? settings.Name)
                .Replace("ForguncyPluginTemplate-GUID", Guid.NewGuid().ToString())
                .Replace("ForguncyPluginTemplate-Description", settings.Description)
                .Replace("ForguncyPluginTemplate-DependenceVersion-Version", forguncyVersion)
                .Replace("ForguncyPluginTemplate", settings.Name);
        }
    }
}
